﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class EmpAddUI : Form
    {
        public EmpAddUI()
        {
            InitializeComponent();

            String sql = "SELECT CmpId,CmpName FROM Company";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            cmbCmp.DataSource = dt;
            cmbCmp.DisplayMember = "CmpName";
            cmbCmp.ValueMember = "CmpId";
            txtEmpId.Focus();

            sql = "SELECT EmpId,EmpName,BankAccount,CmpName,Emplyee.CreateDate,Emplyee.Remark FROM Emplyee "
                            + " LEFT JOIN Company on Emplyee.CmpId = Company.CmpId "
                            + " ORDER BY Emplyee.CreateDate DESC";
            DataTable dtResult = DBSession.ExecuteDataTable(sql, null);
            dgv_EmpInfo.DataSource = dtResult;
        }

        private void btnAddEmp_Click(object sender, EventArgs e)
        {
            String CmpId = cmbCmp.SelectedValue!=null?cmbCmp.SelectedValue.ToString():"";
            String EmpId = txtEmpId.Text.Trim().ToString();
            String EmpName = txtEmpName.Text.Trim().ToString();
            String BankAccount = txtBankAccount.Text.Trim().ToString();
            String CreateDate = DateTime.Now.ToString();
            String remark = txtRemark.Text.Trim().ToString();
            if (CmpId.Length > 0 && EmpId.Length > 0 && EmpName.Length > 0 && BankAccount.Length > 0)
            {
                DataTable dt = DBSession.ExecuteDataTable("SELECT EmpId,EmpName,BankAccount,CmpId,CreateDate,Remark FROM Emplyee WHERE EmpId='"+EmpId+"'",null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    MessageBox.Show("已经有相同的员工ID存在，请检查你要新加入的企业ID号！");
                }
                else
                {
                    String sql = " INSERT INTO Emplyee VALUES('"+EmpId+"','"+EmpName+"','"+BankAccount+"','"+CmpId+"','"+CreateDate+"','"+remark+"');";
                    int count = DBSession.ExecuteNonQuery(sql, null);
                    if (count > 0)
                    {
                        sql = "SELECT EmpId,EmpName,BankAccount,CmpName,Emplyee.CreateDate,Emplyee.Remark FROM Emplyee "
                            + " LEFT JOIN Company on Emplyee.CmpId = Company.CmpId "
                            + " ORDER BY Emplyee.CreateDate DESC";
                        DataTable dtResult = DBSession.ExecuteDataTable(sql, null);
                        dgv_EmpInfo.DataSource = dtResult;
                    }

                    //clear control value.
                    txtEmpId.Text = "";
                    txtEmpName.Text = "";
                    txtBankAccount.Text = "";
                    txtRemark.Text = "";
                    txtEmpId.Focus();
                }
            }
            else
            {
                MessageBox.Show("除备注外，其余信息不能为空！");
            }
        }
    }
}
